#include "FadeToBlack.h"
#include "Screens\GameScreen.h"

FadeToBlack::FadeToBlack(GameState *gameState, float maxTime, bool inverse) : GameObject(gameState){
	setLayer(0);
	m_alpha = 0.0f;
	if(inverse){
		m_alpha = 1.0f;
	}
	m_maxTime = maxTime;
	m_inv = inverse;
}

void FadeToBlack::update(double dt){
	GameScreen *gScreen = static_cast<GameScreen*>(m_screen);

	if(!m_inv){
		m_alpha += (float)dt/m_maxTime;
	}else{
		m_alpha -= (float)dt/m_maxTime;
	}
	if(m_alpha>=1.5f && !m_inv){
		gScreen->restoreCheckPoint();
		kill();
	}else if(m_alpha < 0.0f){
		kill();
	}

	
	gScreen->getMusic().setVolume((1-m_alpha)*50);
}


void FadeToBlack::draw(){
	float width = m_gameState->camera.widthUnits;
	float height = m_gameState->camera.heightUnits;

	glPushMatrix();
	{
		glTranslatef(m_gameState->camera.posX, m_gameState->camera.posY, 0); 
		glScalef(width, height, 1);
		glColor4f(0.0f, 0.0f, 0.0f, m_alpha);
		glBegin(GL_QUADS);
		{
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, 0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, 0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, 0.0f);
		}
		glEnd();
	}
	glPopMatrix();

}


